#include "StdAfx.h"

#include "CmdLineDrawer.h"
#include <iostream>
#include <sstream>
#include <string>

#include "listeners.h"
#include "Floor.h"

#define UP "^^^"
#define DOWN "vvv"

CCmdLineDrawer::CCmdLineDrawer(void)
{
}

CCmdLineDrawer::~CCmdLineDrawer(void)
{
}

const char* CCmdLineDrawer::GetDirectionStr(bool up) const
{
	return up ? UP : DOWN;
}

void CCmdLineDrawer::Draw(IFloorsContainer* floorsContainer)
{
	system("cls");
	std::cout << "------------------------------------------------------------------------------" << std::endl;

	IElevator *pEl = floorsContainer->GetElevator();
	std::string elevatorContent("(");
	elevatorContent += GetDirectionStr(pEl->UpDirection());
	for (PassengerCollection::const_iterator pasIt = pEl->GetLoadedPassengers().begin(); 
		pasIt != pEl->GetLoadedPassengers().end(); 
		++pasIt)
	{
		CPassenger* pas = *pasIt;
		if (pasIt != pEl->GetLoadedPassengers().begin())
			elevatorContent += ", ";

		std::string strTarget;
		std::stringstream ss;
		ss << pas->GetTarget();
		ss >> strTarget;

		elevatorContent += " ";
		elevatorContent += strTarget;
		elevatorContent += " ";
	}
	elevatorContent += GetDirectionStr(pEl->UpDirection());
	elevatorContent += ")";

	std::string emptyString(elevatorContent.length(), ' ');
	for (FloorsCollection::const_reverse_iterator it = floorsContainer->GetFloors().rbegin(); it != floorsContainer->GetFloors().rend(); ++it)
	{
		CFloor* floor = *it;
		std::cout << floor->GetNum() << ":| {";

		if (floor->GetNum() == pEl->GetElevatorPos())
			std::cout << elevatorContent;
		else
			std::cout << emptyString;

		std::cout << "} | Passengers queue: (";			
		for (PassengerCollection::const_iterator pasIt = floor->GetIncomingPassengers().begin(); 
			pasIt != floor->GetIncomingPassengers().end(); 
			++pasIt)
		{
			if (pasIt != floor->GetIncomingPassengers().begin())
				std::cout << ", ";

			std::cout << (*pasIt)->GetTarget();
		}

		std::cout << ")" << std::endl;
	}
	std::cout << "------------------------------------------------------------------------------" << std::endl;

	std::cout << "Step: " << floorsContainer->GetSteps() << std::endl;
	std::cout << "Totally transported: " << floorsContainer->GetTotallyTransported() << " passengers" << std::endl;
	std::cout << "Average wait time: " << floorsContainer->GetElevator()->GetAverageWaitTime() << " steps" << std::endl;
	std::cout << "Average elevator load: " << floorsContainer->GetAverageElevatorLoad() << " persons" << std::endl;
	std::cout << "Elevator capacity: " << pEl->GetCapacity() << " persons" << std::endl;
	std::cout << "Intencivity: " << floorsContainer->GetIntensity() << " new passengers per 10 steps" << std::endl;
}